<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    var socket;
    // 判断当前浏览器是否支持websocket编程
    if (window.WebSocket) {
        socket = new WebSocket("ws://localhost:7000/abc");
        // 相当于channelread0,ev 收到服务器端回显的消息

        socket.onmessage = function (ev) {
            alert(ev.data);
            var rt = document.getElementById("rt");
            console.debug("rt=" + rt);
            rt.value = rt.value + "\n" + ev.data;
        }
        // 相当于链接开启
        socket.onopen = function (ev) {
            var response = document.getElementById("rt");
            response.value = "链接开启~~";
        }

        // 相当于服务器端关闭连接了
        socket.onclose = function (ev) {
            var response = document.getElementById("rt");
            response.value = response.value + "\n" + "链接关闭了~~";
        }
    } else {
        alert("浏览器不支持websocket编程")
    }

    // 发送消息到服务器
    function send(message) {
        // 先判断websocket是否创建好
        if (!window.socket) {
            return;
        }

        if (socket.readyState == WebSocket.OPEN) {
            socket.send(message);
        } else {
            alert("链接没有开启");
        }
    }
</script>
<form onsubmit="return false">
    <textarea name="message" style="width: 300px;height: 300px"></textarea>
    <input type="button" value="发送消息" onclick="send(this.form.message.value)">
    <textarea id="rt" style="height: 300px;width: 300px"></textarea>
    <input type="button" value="清空内容" onclick="document.getElementById('rt').value=''">
</form>
</body>
</html>